class GameFloor extends BaseGroup{
  
    public floorIndex:number = 0;
    public bg:egret.Shape;
    public hardLevel:GameHardLevel;
    public indexLbl:eui.BitmapLabel;
    public imgArray:Array<eui.Image> = [];
    public leftWall:eui.Image;
    public rightWall:eui.Image;
    public bottomWall:eui.Image;
    public bgImg:eui.Image;
    public giftImg:eui.Image;
    // 墙体厚度
    /**
     * 将数据恢复成初始状态
     */
    public resetData(){
        this.floorIndex = 0;
        this.hardLevel = null;
        // 绘制的背景墙
        // if(this.bg && this.bg.parent){
        //     this.bg.parent.removeChild(this.bg);
        // }
        // this.bg = null;
        // 层号
        if(this.indexLbl && this.indexLbl.parent){
            this.indexLbl.parent.removeChild(this.indexLbl);
        }
        this.indexLbl = null;
        // 礼包
        if(this.giftImg && this.giftImg.parent){
            this.giftImg.parent.removeChild(this.giftImg);
        }
        this.giftImg = null;
        // 背景墙上的其他图片
        let len = this.imgArray.length;
        for(let i=0;i<len;i++){
            let img = this.imgArray[i];
            if(img.parent){
                img.parent.removeChild(img);
            }
        }
        this.imgArray = [];
        // 左侧墙的图片
        // if(this.leftWall && this.leftWall.parent){
        //     this.leftWall.parent.removeChild(this.leftWall);
        // }
        // this.leftWall = null;
        // 右侧墙的图片
        // if(this.rightWall && this.rightWall.parent){
        //     this.rightWall.parent.removeChild(this.rightWall);
        // }
        // this.rightWall = null;
        // 墙底部的图片
        // if(this.bottomWall && this.bottomWall.parent){
        //     this.bottomWall.parent.removeChild(this.bottomWall);
        // }
        // this.bottomWall = null;
        // 背景墙的图片
        // if(this.bgImg && this.bgImg.parent){
        //     this.bgImg.parent.removeChild(this.bgImg);
        // }
        // this.bgImg = null;

    }

    public constructor (hardLevel:GameHardLevel){
        super();
        this.hardLevel = hardLevel;
        this.updateViews();

    }
    public updateViews(){
        this.floorIndex = this.hardLevel.floorIndex;
        ViewUtils.setframeOfView(this,this.hardLevel.frame);
        let bgRect = this.drawWall(this.hardLevel.frame);
    
        if(this.floorIndex==0 || (this.floorIndex+1)%10==0){
            // 10的倍数层的特别背景
            if(!this.bgImg){
                this.bgImg = GameConfig.getBgImg(this.hardLevel.floorIndex);
                this.addChildAt(this.bgImg,0);
            }
            ViewUtils.setframeOfView(this.bgImg,new ViewRect(2,-2,this.hardLevel.frame.width-4,this.hardLevel.frame.height-4));
            // 层数
            if(!this.indexLbl){
                this.indexLbl = new eui.BitmapLabel();
                this.indexLbl.font = "num_fnt";
                //this.indexLbl.font = 30;
                this.indexLbl.height = 40;
                this.indexLbl.scaleX = 2;
                this.indexLbl.scaleY = 2;
               // this.indexLbl.verticalAlign = egret.VerticalAlign.MIDDLE;
                this.addChild(this.indexLbl);
            }
            this.indexLbl.text = ""+(this.floorIndex+1);
            // 限时礼包
            this.addGiftPackage();
            

            ViewUtils.setframeOfView(this.indexLbl,new ViewRect(79-GameConfig.floorMargin,(GameConfig.floorHeight-40)/4,100,40));
        }else{
            // 不需要右侧墙的去掉
            GameConfig.saveBgImg(this.bgImg);
            this.bgImg = null;
            if(this.indexLbl && this.indexLbl.parent){
                this.indexLbl.parent.removeChild(this.indexLbl);
            }
            this.indexLbl = null;
        }
        /*
        let transRect = new ViewRect(Utils.randomIntNum(10,300),Utils.randomIntNum(10,20),204*0.2,200*0.2);
        ViewUtils.setframeOfView(this,this.hardLevel.frame);
        this.drawBg(this.hardLevel.frame,[transRect]);
        
        let winImg = new eui.Image();
        winImg.source = "window_png"
        ViewUtils.setframeOfView(winImg,transRect);
        this.addChild(winImg);
        this.imgArray.push(winImg);

        let img1 = new eui.Image();
        img1.source = "safa_png"
        ViewUtils.setframeOfView(img1,new ViewRect(Utils.randomIntNum(10,300),this.hardLevel.frame.height-100,100,100));
        this.addChild(img1);
        this.imgArray.push(img1);

        let img2 = new eui.Image();
        img2.source = "tree_png"
        ViewUtils.setframeOfView(img2,new ViewRect(Utils.randomIntNum(10,300),this.hardLevel.frame.height-10-100,100,100));
        this.addChild(img2);
        this.imgArray.push(img2);
        */
    }
    private addGiftPackage(){
        let index = this.hardLevel.floorIndex+1;
        // 30层必定出现礼包
        // 50或50层以上，出现概率20%
        if(!UserData.hasGiftPackage && (index == 30 || (index>=50 && Math.random()<0.2))){
            this.giftImg = new eui.Image();
            this.giftImg.source = "gift_png";
            this.giftImg.width = 60;
            this.giftImg.height = 60;
            this.addChild(this.giftImg);
            this.giftImg.x = (this.width-this.giftImg.width)/2;
            this.giftImg.y = this.height-this.giftImg.height-GameConfig.floorBorder;
        }
        
    }
    private drawBg(frame:ViewRect,transList:Array<ViewRect>){
        let shape = this.bg = new egret.Shape();
        this.addChildAt(shape,0);
        let bgRect = this.drawWall(frame);
        let wallColor = 0xFAFAF8;
        let transAlpha = 0.1;
        let wallFromX = bgRect.x;
        if(this.floorIndex==0 || (this.floorIndex+1)%10==0){
             // 绘制背景墙
            this.drawEmptyRect(bgRect.x,bgRect.y);
            // this.bg.graphics.beginFill(wallColor,transAlpha);
            // this.bg.graphics.drawRect(bgRect.x,bgRect.y,bgRect.width,bgRect.height);
            // this.bg.graphics.endFill();

            // this.indexLbl = new eui.Label();
            // this.indexLbl.text = this.floorIndex+1+"";
            // this.indexLbl.textColor = GameConfig.TextColors.black;
            // this.indexLbl.italic = true;
            // this.indexLbl.size = 30;
            // ViewUtils.setframeOfView(this.indexLbl,new ViewRect(40,(frame.height-30)/2,100,30));
            // this.addChild(this.indexLbl);
        }
        else if(!transList || transList.length==0){
             // 绘制背景墙
            this.drawEmptyRect(bgRect.x,bgRect.y);
            this.bg.graphics.beginFill(wallColor,1);
            this.bg.graphics.drawRect(bgRect.x,bgRect.y,bgRect.width,bgRect.height);
            this.bg.graphics.endFill();
        }
        else{
            for(let i=0;i<transList.length;i++){
                let transRect = transList[i];
                // 画背景墙
                this.bg.graphics.beginFill(wallColor,1);
                this.bg.graphics.drawRect(wallFromX,bgRect.y,ViewUtils.rightOfRect(transRect)-wallFromX,transRect.y);
                this.bg.graphics.drawRect(wallFromX,transRect.y,transRect.x-wallFromX,transRect.height);
                this.bg.graphics.drawRect(wallFromX,transRect.y+transRect.height,ViewUtils.rightOfRect(transRect)-wallFromX,bgRect.height-ViewUtils.bottomOfRect(transRect));
                this.bg.graphics.endFill();
                wallFromX = ViewUtils.rightOfRect(transRect)-1;

                // 画透明的地方
                // this.bg.graphics.beginFill(wallColor,transAlpha);
                // this.bg.graphics.drawRect(transRect.x,transRect.y,transRect.width,transRect.height);
                // this.bg.graphics.endFill();
            }
            // 闭合背景
            this.drawEmptyRect(wallFromX-1,bgRect.y);
            this.bg.graphics.beginFill(wallColor,1);
            this.bg.graphics.drawRect(wallFromX-0.1,bgRect.y,ViewUtils.rightOfRect(bgRect)-wallFromX,bgRect.height);
            this.bg.graphics.endFill();
        }
        
        
    }
    private drawEmptyRect(x:number,y:number){
        this.bg.graphics.beginFill(0x000000,0);
        //this.bg.graphics.drawRect(x,y,0.1,0.1);
        this.bg.graphics.moveTo(x,y);
        this.bg.graphics.endFill();
    }
    private drawWall(frame:ViewRect):ViewRect{
        let borderWidth = GameConfig.floorBorder;
        let borderColor = 0x000000;
        
        let bgX = 0;
        // 绘制左边
        if(this.hardLevel.hasLeftWall){
            // this.drawEmptyRect(0,0);
            // this.bg.graphics.beginFill(borderColor,1);
            // this.bg.graphics.drawRect(0,0,borderWidth,frame.height-borderWidth);
            // this.bg.graphics.endFill();
            if(!this.leftWall){
                this.leftWall = GameConfig.getVerWall();
                this.addChild(this.leftWall);
            }
            ViewUtils.setframeOfView(this.leftWall,new ViewRect(0,-2,borderWidth,frame.height+2));
            bgX = borderWidth;
        }else{
            // 不需要左侧墙的去掉
            GameConfig.saveVerWall(this.leftWall);
            this.leftWall = null;
        }
        // 绘制右边
        let rightOffset = 0;
        if(this.hardLevel.hasRightWall){
            // this.drawEmptyRect(frame.width-borderWidth,0);
            // this.bg.graphics.beginFill(borderColor,1);
            // this.bg.graphics.drawRect(frame.width-borderWidth,0,borderWidth,frame.height-borderWidth);
            // this.bg.graphics.endFill();
            if(!this.rightWall){
                this.rightWall = GameConfig.getVerWall();
                this.addChild(this.rightWall);
            }
            ViewUtils.setframeOfView(this.rightWall,new ViewRect(frame.width-borderWidth,-2,borderWidth,frame.height+2));
            rightOffset = borderWidth;
        }else{
            // 不需要右侧墙的去掉
            GameConfig.saveVerWall(this.rightWall);
            this.rightWall = null;
        }
        // 绘制底边
        // this.drawEmptyRect(0,frame.height-borderWidth);
        // this.bg.graphics.beginFill(borderColor,1);
        // this.bg.graphics.drawRect(0,frame.height-borderWidth,frame.width,borderWidth);
        // this.bg.graphics.endFill();
        if(!this.bottomWall){
            this.bottomWall = GameConfig.getHorWall();
            this.addChild(this.bottomWall);
        }
        ViewUtils.setframeOfView(this.bottomWall,new ViewRect(bgX-1,frame.height-borderWidth,frame.width-bgX-rightOffset+2,borderWidth));
        // 周围的边
        return new ViewRect(bgX,0,frame.width-bgX-rightOffset,frame.height-borderWidth);
    }
    private moveDownUp(){

    }
    private moveDownDown(){
        if(this.parent){
            this.y = this.y+UserData.roleSpeed.floorDownSpeed;
        }
        
    }
    public addListener() {
		super.addListener();
		App.EventManager.addEventListener(GameConstants.CHANGE_GAME_BG,this.changeBgImg,this);
        App.EventManager.addEventListener(GameConstants.GAME_MVOE_FLOOR_UP,this.moveDownUp,this);
        App.EventManager.addEventListener(GameConstants.GAME_MVOE_FLOOR_DOWN,this.moveDownDown,this);
		
	}
	public removeListener() {
		super.removeListener();
		App.EventManager.removeEventListener(GameConstants.CHANGE_GAME_BG,this.changeBgImg,this);
        App.EventManager.removeEventListener(GameConstants.GAME_MVOE_FLOOR_UP,this.moveDownUp,this);
        App.EventManager.removeEventListener(GameConstants.GAME_MVOE_FLOOR_DOWN,this.moveDownDown,this);
	}

    private changeBgImg(){
        if(this.bgImg){
            this.bgImg.source = "bgRow"+UserData.current_bgId+"_jpg";
        }
    }
}